﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmAccountinfoEdit")]
    public partial class FrmAccountinfoEdit : ModuleForm
    {
        Accountinfo _accountinfo;
        readonly AccountinfoBLL _accountinfoBll;
        readonly SysdictableBLL _sysdictableBLL;
        public FrmAccountinfoEdit()
        {
            InitializeComponent();

            _accountinfoBll = new AccountinfoBLL();
            _sysdictableBLL = new SysdictableBLL();
            _accountinfo = new Accountinfo();
            BindFormContro();
            cboIsbank.SelectedIndex = 0;
        }

        public FrmAccountinfoEdit(Accountinfo obj)
        {
            InitializeComponent();

            _accountinfoBll = new AccountinfoBLL();
            _sysdictableBLL = new SysdictableBLL();
            _accountinfo = obj;

            BindFormContro();
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            cboCurrencyname.DataSource = _sysdictableBLL.Query((int)SysDicEnum.币种);
            cboOpeningbank.DataSource = _sysdictableBLL.Query((int)SysDicEnum.开户银行);

            txtSubjectid.Text = _accountinfo.SubjectName;
            txtSubjectNo.Text = _accountinfo.SubjectNo;
            cboIsbank.Text = _accountinfo.Accounttypename;
            txtAccountname.DataBindings.Add(new Binding("Text", _accountinfo, "Accountname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAccountpym.DataBindings.Add(new Binding("Text", _accountinfo, "Accountpym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtSubjectid.DataBindings.Add(new Binding("Tag", _accountinfo, "Subjectid", true, DataSourceUpdateMode.OnPropertyChanged));
            cboCurrencyname.DataBindings.Add(new Binding("Text", _accountinfo, "Currencyname", true, DataSourceUpdateMode.OnPropertyChanged));
            cboOpeningbank.DataBindings.Add(new Binding("Text", _accountinfo, "Openingbank", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAccountowner.DataBindings.Add(new Binding("Text", _accountinfo, "Accountowner", true, DataSourceUpdateMode.OnPropertyChanged));
            txtBankno.DataBindings.Add(new Binding("Text", _accountinfo, "Bankno", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _accountinfo, "Accountno", true, DataSourceUpdateMode.OnPropertyChanged));

            chIsdefault.Checked = _accountinfo.Isdefault.Value.Equals(1);
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _accountinfo = new Accountinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            cboIsbank.SelectedIndex = 0;
            BindFormContro();
            txtAccountname.Focus();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtAccountname.Text.Length == 0)
            {
                MessageBox.Show(@"账户名称不能为空，请输入.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtAccountname.Focus();
                return;
            }

            _accountinfo.Isdefault = chIsdefault.Checked ? 1 : 0;
            _accountinfo.Accounttypename = cboIsbank.SelectedItem.ToString();
            _accountinfoBll.Edit(_accountinfo);

            MessageBox.Show(@"保存成功.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _accountinfo = new Accountinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            cboIsbank.SelectedIndex = 0;
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_accountinfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_accountinfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _accountinfoBll.PreRecord(id, type, ref isLast);
            _accountinfo = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void cboIsbank_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboOpeningbank.Enabled = txtBankno.Enabled =
                btnOpeningbank.Visible = cboIsbank.SelectedIndex.Equals(1);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        {
            txtAccountpym.Text = ChineseToSpell.GetChineseSpell(txtAccountname.Text);
        }

        private void btnSubjectid_Click(object sender, EventArgs e)
        {
            var f = new FrmSelectAccSubject();
            if (f.ShowDialog() == DialogResult.OK)
            {
                if (f.Tag != null)
                {
                    var obj = (Accountsubject)f.Tag;
                    txtSubjectid.Text = obj.Subjectname;
                    txtSubjectNo.Text = obj.Subjectno;
                    _accountinfo.Subjectid = obj.Id;
                }
            }
        }

        private void btnCurrency_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboCurrencyname, SysDicEnum.币种, isSelectRoot: false);
        }

        private void btnOpeningbank_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboCurrencyname, SysDicEnum.开户银行, isSelectRoot: false);
        }
    }
}
